namespace VoteSystem.Migrations
{
    using System;
    using System.Data.Entity.Migrations;
    
    public partial class init_DB : DbMigration
    {
        public override void Up()
        {
            CreateTable(
                "dbo.UserInfo",
                c => new
                    {
                        UserId = c.Int(nullable: false, identity: true),
                        UserName = c.String(nullable: false, maxLength: 50),
                        DisplayName = c.String(maxLength: 50),
                        Password = c.String(nullable: false, maxLength: 128),
                        LastLoginIPAdress = c.String(maxLength: 20),
                        LastLoginDate = c.DateTime(),
                        IsLock = c.Boolean(nullable: false),
                        RoleId = c.Int(nullable: false),
                        DeptName = c.String(nullable: false),
                    })
                .PrimaryKey(t => t.UserId)
                .ForeignKey("dbo.Roles", t => t.RoleId, cascadeDelete: true)
                .Index(t => t.RoleId);
            
            CreateTable(
                "dbo.Roles",
                c => new
                    {
                        RoleId = c.Int(nullable: false, identity: true),
                        RoleName = c.String(nullable: false, maxLength: 50),
                    })
                .PrimaryKey(t => t.RoleId);
            
            CreateTable(
                "dbo.Positions",
                c => new
                    {
                        Id = c.Int(nullable: false, identity: true),
                        DeptName = c.String(),
                        PositionName = c.String(),
                    })
                .PrimaryKey(t => t.Id);
            
            CreateTable(
                "dbo.Employees",
                c => new
                    {
                        EmployeeId = c.Int(nullable: false, identity: true),
                        Name = c.String(maxLength: 50),
                        Age = c.String(maxLength: 20),
                        Education = c.String(maxLength: 50),
                        Major = c.String(maxLength: 50),
                        Mobile = c.String(maxLength: 50),
                        Sex = c.String(maxLength: 20),
                    })
                .PrimaryKey(t => t.EmployeeId);
            
            CreateTable(
                "dbo.VoteConfigs",
                c => new
                    {
                        Id = c.Int(nullable: false, identity: true),
                        Count = c.Int(nullable: false),
                        StartTime1 = c.DateTime(nullable: false),
                        EndTime1 = c.DateTime(nullable: false),
                        StartTime2 = c.DateTime(nullable: false),
                        EndTime2 = c.DateTime(nullable: false),
                        StartTime3 = c.DateTime(nullable: false),
                        EndTime3 = c.DateTime(nullable: false),
                        Active = c.Boolean(nullable: false),
                    })
                .PrimaryKey(t => t.Id);
            
            CreateTable(
                "dbo.EmployeeVotePositions",
                c => new
                    {
                        Id = c.Int(nullable: false, identity: true),
                        EmployeeId = c.Int(nullable: false),
                        ConfigId = c.Int(nullable: false),
                        PositionId1 = c.Int(nullable: false),
                        PositionId2 = c.Int(nullable: false),
                        PositionId3 = c.Int(nullable: false),
                    })
                .PrimaryKey(t => t.Id)
                .ForeignKey("dbo.Employees", t => t.EmployeeId, cascadeDelete: true)
                .ForeignKey("dbo.VoteConfigs", t => t.ConfigId, cascadeDelete: true)
                .ForeignKey("dbo.Positions", t => t.PositionId1, cascadeDelete: false)
                .ForeignKey("dbo.Positions", t => t.PositionId2, cascadeDelete: false)
                .ForeignKey("dbo.Positions", t => t.PositionId3, cascadeDelete: false)
                .Index(t => t.EmployeeId)
                .Index(t => t.ConfigId)
                .Index(t => t.PositionId1)
                .Index(t => t.PositionId2)
                .Index(t => t.PositionId3);
            
            CreateTable(
                "dbo.SysConfigs",
                c => new
                    {
                        Id = c.Int(nullable: false, identity: true),
                        ImportPosition = c.Boolean(nullable: false),
                    })
                .PrimaryKey(t => t.Id);
            
            CreateTable(
                "dbo.VoteResults",
                c => new
                    {
                        Id = c.Int(nullable: false, identity: true),
                        VoteConfigId = c.Int(nullable: false),
                        PositionId = c.Int(nullable: false),
                        EmployeeId = c.Int(nullable: false),
                    })
                .PrimaryKey(t => t.Id)
                .ForeignKey("dbo.VoteConfigs", t => t.VoteConfigId, cascadeDelete: true)
                .ForeignKey("dbo.Positions", t => t.PositionId, cascadeDelete: true)
                .ForeignKey("dbo.Employees", t => t.EmployeeId, cascadeDelete: true)
                .Index(t => t.VoteConfigId)
                .Index(t => t.PositionId)
                .Index(t => t.EmployeeId);
            
        }
        
        public override void Down()
        {
            DropIndex("dbo.VoteResults", new[] { "EmployeeId" });
            DropIndex("dbo.VoteResults", new[] { "PositionId" });
            DropIndex("dbo.VoteResults", new[] { "VoteConfigId" });
            DropIndex("dbo.EmployeeVotePositions", new[] { "PositionId3" });
            DropIndex("dbo.EmployeeVotePositions", new[] { "PositionId2" });
            DropIndex("dbo.EmployeeVotePositions", new[] { "PositionId1" });
            DropIndex("dbo.EmployeeVotePositions", new[] { "ConfigId" });
            DropIndex("dbo.EmployeeVotePositions", new[] { "EmployeeId" });
            DropIndex("dbo.UserInfo", new[] { "RoleId" });
            DropForeignKey("dbo.VoteResults", "EmployeeId", "dbo.Employees");
            DropForeignKey("dbo.VoteResults", "PositionId", "dbo.Positions");
            DropForeignKey("dbo.VoteResults", "VoteConfigId", "dbo.VoteConfigs");
            DropForeignKey("dbo.EmployeeVotePositions", "PositionId3", "dbo.Positions");
            DropForeignKey("dbo.EmployeeVotePositions", "PositionId2", "dbo.Positions");
            DropForeignKey("dbo.EmployeeVotePositions", "PositionId1", "dbo.Positions");
            DropForeignKey("dbo.EmployeeVotePositions", "ConfigId", "dbo.VoteConfigs");
            DropForeignKey("dbo.EmployeeVotePositions", "EmployeeId", "dbo.Employees");
            DropForeignKey("dbo.UserInfo", "RoleId", "dbo.Roles");
            DropTable("dbo.VoteResults");
            DropTable("dbo.SysConfigs");
            DropTable("dbo.EmployeeVotePositions");
            DropTable("dbo.VoteConfigs");
            DropTable("dbo.Employees");
            DropTable("dbo.Positions");
            DropTable("dbo.Roles");
            DropTable("dbo.UserInfo");
        }
    }
}
